package com.zzy.controller;

import com.zzy.model.AjaxMsg;
import com.zzy.mq.Sender;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping(value = "/mq/")
public class MqController {
    // spring boot 为我们提供的包装类
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    private Sender sender;

    @RequestMapping("send")
    @ResponseBody
    public AjaxMsg send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        // 调用 发送消息的方法
        rabbitTemplate.convertAndSend("direct", context);
        System.out.println("发送成功！");
        return AjaxMsg.returnAjaxMsg();
    }

    @RequestMapping("send2")
    @ResponseBody
    public AjaxMsg send2() {
        sender.send();
        return AjaxMsg.returnAjaxMsg();
    }

    @RequestMapping("send3")
    @ResponseBody
    public AjaxMsg send3(HttpServletRequest request) {
        String msg = request.getParameter("msg");
        sender.send(msg);
        return AjaxMsg.returnAjaxMsg(msg);
    }


    @RequestMapping("test")
    @ResponseBody
    public AjaxMsg test() {
        return AjaxMsg.returnAjaxMsg("test");
    }
}
